package com.project.controller;

import com.project.bean.BuyCartBean;
import com.project.bean.ProductBean;
import com.project.bean.UserBean;
import com.project.service.IBuyCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("buyCart")
@Api(tags = "采购车控制器")
public class BuyCartController {
    @Autowired
    private IBuyCartService service;

    @RequiresPermissions("buy:add")
    @RequestMapping("findBuyCartByUserId")
    @ApiOperation(value = "采购车分页", notes = "根据登录用户分页展示采购车")
    public List<BuyCartBean> findBuyCartBYUserId(HttpServletRequest req) {
        UserBean user = (UserBean) req.getSession().getAttribute("loginUser");
        return service.findBuyCartByUserId(user.getUserId());
    }

    @RequiresPermissions("buy:add")
    @RequestMapping("updateBuyPrice")
    @ApiOperation(value = "改变采购价", notes = "改变采购价")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "buyCartId", value = "采购车id"),
            @ApiImplicitParam(name = "newBuyPrice", value = "改变后的采购价")
    })
    public String updateBuyPrice(Integer buyCartId, Integer newBuyPrice) {
        service.updateBuyPrice(buyCartId, newBuyPrice);
        return "ok";
    }

    @RequiresPermissions("buy:add")
    @RequestMapping("updateBuyNum")
    @ApiOperation(value = "改变采购数量", notes = "改变采购数量")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "buyCartId", value = "采购车id"),
            @ApiImplicitParam(name = "newBuyNum", value = "改变后的采购数量")
    })
    public String updateBuyNum(Integer buyCartId, Integer newBuyNum) {
        service.updateBuyNum(buyCartId, newBuyNum);
        return "ok";
    }

    @RequiresPermissions("buy:add")
    @RequestMapping("delBuyCart")
    @ApiOperation(value = "删除某个采购车", notes = "删除某个采购车")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "buyCartId", value = "采购车id"),
    })
    public String delBuyCart(Integer buyCartId) {
        service.delBuyCart(buyCartId);
        return "ok";
    }

    @RequiresPermissions("buy:add")
    @RequestMapping("in")
    @ApiOperation(value = "入库", notes = "入库")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "sName", value = "供应商名称"),
    })
    public String in(String sName, HttpServletRequest req) {
        UserBean user = (UserBean) req.getSession().getAttribute("loginUser");
        if (sName != null && sName != "") {
            service.in(user.getUserId(), sName);
            return "ok";
        }
        return "no";
    }

    @RequiresPermissions("buy:add")
    @RequestMapping("addBuyCart")
    @ApiOperation(value = "添加采购商品", notes = "添加采购商品")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "productId", value = "商品编号"),
            @ApiImplicitParam(name = "pName", value = "商品名称"),
            @ApiImplicitParam(name = "brand", value = "品牌"),
            @ApiImplicitParam(name = "type", value = "类型"),
            @ApiImplicitParam(name = "imgPath", value = "图片路径"),
            @ApiImplicitParam(name = "buyNum", value = "采购数量")
    })
    public String addBuyCart(String productId, String pName, String brand, String type, String imgPath, Integer buyNum, HttpServletRequest req) {
        UserBean user = (UserBean) req.getSession().getAttribute("loginUser");
        service.addBuyCart(new ProductBean(productId, pName, brand, type, imgPath), user.getUserId(), buyNum);
        return "ok";
    }

    @RequiresPermissions("buy:add")
    @RequestMapping("/addImg")
    @ApiOperation(value = "上传商品图片", notes = "上传商品图片")
    public String addImg(@RequestParam("imgFile") MultipartFile mf) throws IOException {
        //上传到指定文件名
        String fileName = mf.getOriginalFilename();
        fileName = System.currentTimeMillis() + fileName;
        mf.transferTo(new File("C:/Users/LiuT/Desktop/vaadin/storesystem1/src/main/resources/static/img/" + fileName));
        return fileName;
    }
}
